/**
 * common
 */
$(function(){
    /*根据url展开对应侧边栏*/
    var currentUrl = window.location.pathname;
    var active = $('#side-menu').find('a[href="' + currentUrl + '"]');
    active.closest('li').addClass('active');
    active.closest('li').parents('.treeview').addClass('active');

    /*加载iCheck*/
    $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
        checkboxClass: 'icheckbox_minimal-blue',
        radioClass: 'iradio_minimal-blue'
    });

    /*checkbox全选-反选*/
    $('.checkbox-toggle').on('ifChecked', function (event) {
        var _this = $(this);
        var _table = _this.closest('.table');
        _table.find("tr td input[type='checkbox']").iCheck("check");
    });
    $('.checkbox-toggle').on('ifUnchecked', function (event) {
        var _this = $(this);
        var _table = _this.closest('.table');
        _table.find("tr td input[type='checkbox']").iCheck("uncheck");
    });

    //状态status列表修改（只能进行0和1值的切换）
    $('body').off('click', 'td a.changeStatus');
    $('body').on('click', 'td a.changeStatus', function(event){
        var addclass;
        var removeclass;
        var pvalue;   //提交值
        var _this = $(this);
        var field = _this.data('field');
        var id = _this.data('id');
        var value = _this.data('value');
        var url = _this.data('url');
        if ( value === 1){
            pvalue = 0;
            addclass = "fa-check-circle text-green";
            removeclass = "fa-times-circle text-red";
        }else{
            pvalue = 1;
            addclass = "fa-times-circle text-red";
            removeclass = "fa-check-circle text-green";
        }
        var dataStr = jQuery.parseJSON( '{"id":"'+id+'","'+field+'":"'+pvalue+'"}' );   //字符串转json
        $.ajax({
            type : "post",
            url : url,
            dataType : 'json',
            data : dataStr,
            success : function(data) {
                if(data.code === 1){
                    _this.data('value', pvalue);
                    _this.removeClass(addclass);
                    _this.addClass(removeclass);
                    layer.msg(data.msg, {icon: 6});
                }else (
                    layer.msg(data.msg, {icon: 5})
                )
            }
        });
    });

    //多选、单选数据处理
    $('body').off('click', '.delete-one,.delete-all');
    $('body').on("click", '.delete-one,.delete-all', function(event){
        event.preventDefault();
        var _this = $(this);
        var title = _this.data('title')?_this.data('title'):'删除';
        var url_del = _this.data('url')||'';
        var message = _this.data('message')?_this.data('message'):'确认操作？';
        if(_this.hasClass('delete-all')){   //批量删除
            var id = '';
            var str = '';
            var table_box = _this.closest('.box-header').next('.box-body').find(".table tr td input[name='id[]']");
            $(table_box).each(function(){
                if(true == $(this).is(':checked')){
                    str += $(this).val() + ",";
                }
            });
            if(str.substr(str.length-1)== ','){
                id = str.substr(0, str.length-1);
            }
        }else{                              //单条删除
            var id = _this.data('id')||'';
        }
        if(id && url_del){
            BootstrapDialog.confirm({
                onshow:function(obj){
                    var cssConf = {};
                    cssConf['width']=300;
                    if(cssConf){
                        obj.getModal().find('div.modal-dialog').css(cssConf);
                    }
                },
                title: title,
                message: message,
                btnCancelLabel: '取消',
                btnOKLabel: '确定',
                callback: function(resultDel) {
                    if(resultDel === true) {
                        $.ajax({
                            type : "post",
                            url : url_del,
                            dataType : 'json',
                            data : { id:id, },
                            success : function(data) {
                                if(data.status == '1'){
                                    $.amaran({'message':data.info});
                                    var url = data.url;
                                    $.pjax({url: url, container: '#pjax-container', fragment:'#pjax-container'})
                                }else if(data.status == '2'){
                                    restlogin(data.info);
                                }else{
                                    $.amaran({'message':data.info});
                                }
                            }
                        });
                    }
                }
            });
        }
    });
});